<?php

/**
 * This is the model class for table "{{usuario}}".
 *
 * The followings are the available columns in table '{{usuario}}':
 * @property integer $id_usuario
 * @property string $nome
 * @property string $email
 * @property string $login
 * @property string $senha
 * @property string $empresa
 * @property string $cidade
 * @property string $estado
 * @property string $regiao
 * @property integer $admin
 *
 * The followings are the available model relations:
 * @property Resultado[] $resultados
 */
class Usuario extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Usuario the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{usuario}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nome, email, login, senha, empresa, cidade, estado, regiao', 'required'),
			array('admin', 'numerical', 'integerOnly'=>true),
			array('nome, email', 'length', 'max'=>150),
			array('login, senha, cidade, regiao', 'length', 'max'=>45),
			array('empresa', 'length', 'max'=>75),
			array('estado', 'length', 'max'=>2),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_usuario, nome, email, login, senha, empresa, cidade, estado, regiao, admin', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'resultados' => array(self::HAS_MANY, 'Resultado', 'id_usuario'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_usuario' => 'Id Usuario',
			'nome' => 'Nome',
			'email' => 'Email',
			'login' => 'Login',
			'senha' => 'Senha',
			'empresa' => 'Empresa',
			'cidade' => 'Cidade',
			'estado' => 'Estado',
			'regiao' => 'Regiao',
			'admin' => 'Admin',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_usuario',$this->id_usuario);
		$criteria->compare('nome',$this->nome,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('login',$this->login,true);
		$criteria->compare('senha',$this->senha,true);
		$criteria->compare('empresa',$this->empresa,true);
		$criteria->compare('cidade',$this->cidade,true);
		$criteria->compare('estado',$this->estado,true);
		$criteria->compare('regiao',$this->regiao,true);
		$criteria->compare('admin',$this->admin);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function beforeSave() 
        {
            if(parent::beforeSave())
            {
                if(!empty($this->senha))
                    $this->senha = md5($this->senha . Yii::app()->params["saltString"]);
            }
            
            return true;
        }        
}